home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ded / getname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1984-04-25  |  3.4 KB  |  184 lines

  1. /* More or less lifted from ps.c per dlw@Berkeley.  This should be in libc! */
  2.  
  3. #ifndef lint
  4. static char RCSid[] =
  5. "$Header: getname.c,v 1.4 84/04/25 03:55:55 lepreau Exp $";
  6. #endif
  7.  
  8. #include <pwd.h>
  9. #include <grp.h>
  10. #include <utmp.h>
  11.  
  12. struct    utmp utmp;
  13.  
  14. #ifndef NUID
  15. # define NUID    1024    /* must not be a multiple of 5 */
  16. #endif NUID
  17.  
  18. #ifndef NGID
  19. # define NGID    301    /*   ''   */
  20. #endif
  21.  
  22. #define    NMAX    (sizeof (utmp.ut_name))
  23. #define NULL 0
  24.  
  25. extern char *strncpy();
  26.  
  27. struct nametable {
  28.     char    nt_name[NMAX];
  29.     int    nt_id;
  30. };
  31. struct nametable    unames[NUID];
  32. /*
  33.  * Doing it this way saves 4 byes per entry in the nametable, but loses
  34.  * in that the return value gets clobbered next time.  Latter doesn't matter
  35.  * to ded.
  36.  */
  37. char nplus1[NMAX+1];
  38.  
  39. struct nametable *
  40. findslot (id, tbl, len)
  41.     unsigned short    id;
  42.     struct nametable *tbl;
  43.     int        len;
  44. {
  45.     register struct nametable    *nt, *nt_start;
  46.  
  47.     /*
  48.      * find the id or an empty slot.
  49.      * return NULL if neither found.
  50.      */
  51.  
  52.     nt = nt_start = tbl + (id % (len - 20));
  53.     while (nt->nt_name[0] && nt->nt_id != id)
  54.     {
  55.         if ((nt += 5) >= &tbl[len])
  56.             nt -= len;
  57.         if (nt == nt_start)
  58.             return((struct nametable *)NULL);
  59.     }
  60.     return(nt);
  61. }
  62.  
  63. /*
  64.  * find uid in hashed table; add it if not found.
  65.  * return pointer to name.
  66.  */
  67. char *
  68. getname(uid)
  69.     unsigned short    uid;
  70. {
  71.     register struct passwd        *pw;
  72.     static int            init = 0;
  73.     struct passwd            *getpwent();
  74.     register struct nametable    *n;
  75.     static lastuid = -1;
  76.  
  77.     if (uid == lastuid)
  78.         return (nplus1);
  79.     
  80.     if ((n = findslot(uid, unames, NUID)) == NULL)
  81.         return((char *)NULL);
  82.  
  83.     if (n->nt_name[0]) {    /* occupied? */
  84.         (void) strncpy (nplus1, n->nt_name, NMAX);
  85.         lastuid = uid;
  86.         return (nplus1);
  87.     }
  88.     
  89.     switch (init)
  90.     {
  91.         case 0:
  92.             (void) setpwent();
  93.             init = 1;
  94.             /* intentional fall-thru */
  95.         case 1:
  96.             while (pw = getpwent())
  97.             {
  98.                 if (pw->pw_uid < 0)
  99.                     continue;
  100.                 n = findslot((unsigned short)pw->pw_uid,
  101.                     unames, NUID);
  102.                 if (n == NULL)
  103.                 {
  104.                     (void) endpwent();
  105.                     init = 2;
  106.                     return((char *)NULL);
  107.                 }
  108.                 if (n->nt_name[0])
  109.                     continue;   /* duplicate, not uid */
  110.                 (void) strncpy(n->nt_name, pw->pw_name, NMAX);
  111.                 n->nt_id = pw->pw_uid;
  112.                 if (pw->pw_uid == uid) {
  113.                     (void) strncpy (nplus1, n->nt_name, NMAX);
  114.                     lastuid = uid;
  115.                     return (nplus1);
  116.                 }
  117.             }
  118.             (void) endpwent();
  119.             init = 2;
  120.             /* intentional fall-thru */
  121.         case 2:
  122.             return ((char *)NULL);
  123.     }
  124.     /*NOTREACHED*/
  125. }
  126.  
  127. #ifdef notyet
  128. struct nametable    gnames[NGID];
  129.  
  130. /*
  131.  * find gid in hashed table; add it if not found.
  132.  * return pointer to name.
  133.  */
  134. char *
  135. getgroup (gid)
  136.     unsigned short    gid;
  137. {
  138.     register struct group    *gr;
  139.     static int    init = 0;
  140.     struct group    *getgrent();
  141.     register struct nametable    *n;
  142.  
  143.     if ((n = findslot(gid, gnames, NGID)) == NULL)
  144.         return((char *)NULL);
  145.  
  146.     if (n->nt_name[0])    /* occupied? */
  147.         return(n->nt_name);
  148.  
  149.     switch (init)
  150.     {
  151.         case 0:
  152.             (void) setgrent();
  153.             init = 1;
  154.             /* intentional fall-thru */
  155.         case 1:
  156.             while (gr = getgrent())
  157.             {
  158.                 if (gr->gr_gid < 0)
  159.                     continue;
  160.                 n = findslot((unsigned short) gr->gr_gid,
  161.                     gnames, NGID);
  162.                 if (n == NULL)
  163.                 {
  164.                     (void) endgrent();
  165.                     init = 2;
  166.                     return((char *)NULL);
  167.                 }
  168.                 if (n->nt_name[0])
  169.                     continue;    /* duplicate, not gid */
  170.                 (void) strncpy(n->nt_name, gr->gr_name, NMAX);
  171.                 n->nt_id = gr->gr_gid;
  172.                 if (gr->gr_gid == gid)
  173.                     return (n->nt_name);
  174.             }
  175.             (void) endgrent();
  176.             init = 2;
  177.             /* intentional fall-thru */
  178.         case 2:
  179.             return ((char *)NULL);
  180.     }
  181.     /*NOTREACHED*/
  182. }
  183. #endif notyet
  184.